package com.woniu.pay.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.woniu.pay.constant.AliPayInfoConstants;
import com.woniu.pay.entity.PayInfo;
import com.woniu.pay.entity.RefundInfo;
import com.woniu.pay.service.AliPayService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DecimalFormat;

@Service
@Slf4j
public class AliPayServiceImpl implements AliPayService {

    @Resource
    private AlipayClient alipayClient;

    @Override
    public String pay(PayInfo payInfo) throws AlipayApiException {
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        //异步接收地址，仅支持http/https，公网可访问
        request.setNotifyUrl(payInfo.getNotifyUrl());
        //同步跳转地址，仅支持http/https
        request.setReturnUrl(payInfo.getReturnUrl());
        /******必传参数******/
        JSONObject bizContent = new JSONObject();
        //商户订单号，商家自定义，保持唯一性
        bizContent.put("out_trade_no", payInfo.getOutTradeNo());
        //支付金额，最小值0.01元
        DecimalFormat df = new DecimalFormat("#.00");
        bizContent.put("total_amount", df.format(payInfo.getTotalAmount()));
        //订单标题，不可使用特殊符号
        bizContent.put("subject", payInfo.getSubject());
        //电脑网站支付场景固定传值FAST_INSTANT_TRADE_PAY
        bizContent.put("product_code", AliPayInfoConstants.PRODUCT_CODE);

        /******可选参数******/
        //bizContent.put("time_expire", "2022-08-01 22:00:00");

        //// 商品明细信息，按需传入
        //JSONArray goodsDetail = new JSONArray();
        //JSONObject goods1 = new JSONObject();
        //goods1.put("goods_id", "goodsNo1");
        //goods1.put("goods_name", "子商品1");
        //goods1.put("quantity", 1);
        //goods1.put("price", 0.01);
        //goodsDetail.add(goods1);
        //bizContent.put("goods_detail", goodsDetail);

        //// 扩展信息，按需传入
        //JSONObject extendParams = new JSONObject();
        //extendParams.put("sys_service_provider_id", "2088511833207846");
        //bizContent.put("extend_params", extendParams);

        request.setBizContent(bizContent.toString());
        AlipayTradePagePayResponse response = alipayClient.pageExecute(request);
        if(response.isSuccess()){
            System.out.println("调用成功");
            log.debug("{}",response.getBody());
        } else {
            System.out.println("调用失败");
        }
        return response.getBody();
    }

    @SneakyThrows
    @Override
    public AlipayTradeRefundResponse refund(RefundInfo refundInfo) {
        AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", refundInfo.getOutTradeNo());
        DecimalFormat df = new DecimalFormat("#.00");
        bizContent.put("refund_amount", df.format(refundInfo.getRefundAmount()));
//        bizContent.put("out_request_no", "HZ01RF001");

        //// 返回参数选项，按需传入
        //JSONArray queryOptions = new JSONArray();
        //queryOptions.add("refund_detail_item_list");
        //bizContent.put("query_options", queryOptions);

        request.setBizContent(bizContent.toString());
        AlipayTradeRefundResponse response = alipayClient.execute(request);
        if(response.isSuccess()){
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
        return response;
    }

    @SneakyThrows
    @Override
    public AlipayTradeQueryResponse queryStatusByOutTradeNo(String outTradeNo) {
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", outTradeNo);
//bizContent.put("trade_no", "2014112611001004680073956707");
        request.setBizContent(bizContent.toString());
        AlipayTradeQueryResponse response = alipayClient.execute(request);
        if(response.isSuccess()){
            System.out.println("调用成功");
            log.debug("交易查询接口调用成功，交易的订单编号为：{}，交易的支付宝流水号为：{}，交易的状态为：{}",
                    outTradeNo, response.getTradeNo(), response.getTradeStatus());
        } else {
            System.out.println("调用失败");
        }
        return response;
    }
}
